package com.baidu.erp.sercvice.impl;

import com.baidu.erp.composite.PermissionNode;
import com.baidu.erp.dao.PermissionDao;
import com.baidu.erp.dao.RolePermissionRelationDao;
import com.baidu.erp.domain.Permission;
import com.baidu.erp.sercvice.PermissionService;
import com.baidu.erp.utils.DateUtil;
import com.baidu.erp.utils.MybatisUtil;
import com.baidu.erp.visitor.CheckRolePermissionRelationVisitor;
import com.baidu.erp.visitor.DeletePermissionVisitor;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Optional;

public class PermissionServiceImpl implements PermissionService {

    @Override
    public List<Permission> getAll() {
        //建立工场并开启会话
        SqlSession sqlSession = MybatisUtil.getsqlSession();
        //3.获取代理对象
        PermissionDao dao = sqlSession.getMapper(PermissionDao.class);
        //4.执行语句
        return dao.getAll();
    }

    @Override
    public Permission getPermissionById(String id) {
        SqlSession sqlSession = MybatisUtil.getsqlSession();
        PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
        return permissionDao.getPermissionById(id);
    }

    @Override
    public List<Permission> getSonPermissionById(String id) {
        SqlSession sqlSession = MybatisUtil.getsqlSession();
        PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
        return permissionDao.getSonPermissionById(id);
    }

    @Override
    public Optional<Permission> getPermissionNameAndPid(String pId, String name) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getsqlSession();
            PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
            Permission permission = permissionDao.getPermissionNameAndPid(pId,name);
            return Optional.ofNullable(permission);
        }catch (Exception e) {
            e.printStackTrace();
            return Optional.empty();
        }
    }

    @Override
    public List<Permission> getPermissionByRoleId(String roleId) {
        SqlSession sqlSession = MybatisUtil.getsqlSession();
        PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
        return permissionDao.getPermissionByRoleId(roleId);
    }

    @Override
    public int save(Permission permission) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getsqlSession();
            PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
            permission.setCreateTime(DateUtil.getCurrentTime());
            int count = permissionDao.save(permission);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return 0;
        }
    }

    @Override
    public int del(String id) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getsqlSession();
            PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);

//            PermissionService permissionService = new PermissionServiceImpl();
//            List<Permission> permissionList = permissionService.getSonPermissionById(id);
//            String sonId = null;
//            for (Permission permission : permissionList) {
//                sonId = permission.getId();
//                this.del(sonId);
//            }

            int count = permissionDao.del(id);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return 0;
        }
    }

    @Override
    public int delete(String id) {
        SqlSession sqlSession = MybatisUtil.getsqlSession();
        PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
        Permission permission = permissionDao.getPermissionById(id);
        PermissionNode permissionNode = null;
        try {
            permissionNode = permission.clone(PermissionNode.class);
            RolePermissionRelationDao roletionDao = sqlSession.getMapper(RolePermissionRelationDao.class);
            //检查权限是否被占用的访问者
            CheckRolePermissionRelationVisitor checkRelationVisitor = new CheckRolePermissionRelationVisitor(permissionDao,roletionDao);
            checkRelationVisitor.visit(permissionNode);
            //删除权限的访问者
            DeletePermissionVisitor deleteVisitor = new DeletePermissionVisitor(permissionDao,sqlSession);

            Boolean status = checkRelationVisitor.getStatus();
            if (!status){
                deleteVisitor.visit(permissionNode);
                return 1;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }


    @Override
    public int update(Permission permission) {
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtil.getsqlSession();
            PermissionDao permissionDao = sqlSession.getMapper(PermissionDao.class);
            permission.setEditTime(DateUtil.getCurrentTime());
            int count = permissionDao.update(permission);
            sqlSession.commit();
            return count;
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            return 0;
        }
    }


}
